﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using ComicIsland.Common;
using ComicIsland.Common.Entities;

/**
 * 创建时间：2014/12/18 17:15:55
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.App.DownloadConverter
{
    /// <summary>
    /// 漫画对应章节中的图片总数
    /// </summary>
    public class ComicChapterImageTotalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int result = 0;
            try
            {
                string comicAndPartId = string.Empty;
                if (value != null)
                {
                    comicAndPartId = value.ToString();
                }

                if (!string.IsNullOrWhiteSpace(comicAndPartId))
                {
                    var cpIds = comicAndPartId.Split('#');
                    if (cpIds != null && cpIds.Length == 2)
                    {
                        int comicId = 0, partId = 0;
                        int.TryParse(cpIds[0], out comicId);
                        int.TryParse(cpIds[1], out partId);

                        var items = App.comicDbHelper.ImageGetByComicIdAndChapterId(comicId, partId);
                        if (items != null && items.Count > 0)
                        {
                            result = items.Count;
                        }
                    }
                }
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
